﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    ///// <summary> 单例 </summary>
    
    [HideInInspector]
    public float h;//水平移动的距离
    [HideInInspector]
    public float v;//竖直移动的距离
    private GameObject temp;
    #region 角色数值初始化
    /// <summary> 判断子弹射出的位置(true-脚部，false-手部) </summary>
    public bool rootShot = false;
    /// <summary> 子弹速度 </summary>
    public static float bulletSpeed = 6;
    /// <summary> 角色初始发射弹数 </summary>
    public int shotCount = 1;
    /// <summary> 角色初始等级 </summary>
    public int lvl = 1;
    /// <summary> 角色初始血量 </summary>
    public float hp = 10;
    /// <summary>角色血量上限 </summary>
    public float max_hp = 10;
    /// <summary> 角色初始攻击 </summary>
    public float atk = 2;
    /// <summary> 角色初始防御 </summary>
    public float def = 0;
    /// <summary> 角色初始射程 </summary>
    public float atkRange = 10;
    /// <summary> 角色初始移动速度 </summary>
    public float speed = 5;
    /// <summary> 角色初始拾取范围 </summary>
    public float pickUpRange = 0.7f;

    /// <summary> 角色初始经验值 </summary>
    public float exp = 1;
    /// <summary> 角色初始经验球吸收值 </summary>
    public float expBall_value = 1;
    /// <summary> 角色初始经验槽上限 </summary>
    public float max_exp = 10;
    #endregion
    #region 获取物体 
    private Transform playerPos;  
    public PlayerBase playerBases = new PlayerBase();

    /// <summary> 缓存子弹物体 </summary>
    private GameObject EnemyBulletTemp;
    /// <summary> 游戏角色对象的空物体 </summary>
    public Enemy EnemyCharacter;
    /// <summary> 角色模型 </summary>
    public GameObject[] PlayerPrefab;
    /// <summary> 角色根对象 </summary>
    public GameObject root;
    /// <summary> 角色地面位置产生的物体 </summary>
    public List<GameObject> rootPrefabs;
    /// <summary> 子弹预制体 </summary>
    public GameObject bulletPrefab;
    /// <summary> 子弹生成位置 </summary>
    public Transform bulletPos;
    /// <summary> UI血条 </summary>
    public Slider lifbar;
    /// <summary> 死亡后特效 /// </summary>
    public GameObject DiedEffect;

    #endregion
    private GameObject enemychara; //初始化后的角色
    GameObject p;
   
    public string EnemyName;
    public int score=0;
    public GameObject empiricalBall;
    
    void Start()
    {        
        #region 初始数值设定
        playerBases.shotCount = shotCount;
        playerBases.lvl = lvl;
        playerBases.hp = hp;
        playerBases.max_hp = max_hp;
        playerBases.atk = atk;
        playerBases.def = def;
        playerBases.atkRange = atkRange;
        playerBases.speed = speed;
        playerBases.pickUpRange = pickUpRange;
        playerBases.exp = exp;
        playerBases.expBall_value = expBall_value;
        playerBases.max_exp = max_exp;
       // setPickUpRange(pickUpRange);
        rootShot = false;
        // rootPrefabs.Clear();
        #endregion

        int temp = Random.Range(0, GameManger.Insatance.nameList.Count);
        EnemyName = GameManger.Insatance.nameList[temp];
        GameManger.Insatance.nameList.RemoveAt(temp);
        EnemyBulletTemp = GameObject.Find("EnemyBulletTemp");
        int r = Random.Range(0, PlayerPrefab.Length);
        enemychara = PlayerPrefab[r];
        Instantiate(enemychara, EnemyCharacter.transform.GetChild(0));

        Physics.IgnoreCollision(GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>(), this.GetComponent<CapsuleCollider>(), true);
    }

    // Update is called once per frame
    void Update()
    {  
        lifbar.transform.LookAt(GameObject.Find("Main Camera").transform);
        //Vector3 temp = new Vector3 (transform.position.x, -1.2f, transform.position.z);
         //transform.position = temp;
        //transform.LookAt(playerPos.position);//看向玩家
        //transform.Translate(transform.forward * Time.deltaTime * Speed, Space.World);//向前移动
    }

    /// <summary> 角色受到攻击 </summary>
    /// <param name="damage">伤害数值</param>
    public void GetAttack(float damage)
    {
        if (hp < 0) return;
        hp -= damage; //扣血
        lifbar.value = hp / max_hp; //显示血量
        if (hp <= 0)
        {
            PlayerDied();
        }
    }

    /// <summary>
    /// 攻击
    /// </summary>
    public void Attack()
    {
        //判断子弹生成位置
        Transform shotPlace;
        if (rootShot)
            shotPlace = root.transform;
        else
            shotPlace = bulletPos.transform;

        #region 子弹数量展开计算
        float angle;//子弹展开的总角度，随子弹数量增加

        if (shotCount == 1)
            angle = 0;//不偏移
        else
            angle = 10 * shotCount;//计算偏移角度

        float angleOffset = angle / shotCount;//子弹之间角度偏移
        Quaternion bulletRot;
        for (int i = 0; i < shotCount; i++)
        {
            bulletRot = shotPlace.rotation * Quaternion.Euler(0, angle / 2 - angleOffset * i, 0);
            GameObject b = Instantiate(bulletPrefab, shotPlace.position, bulletRot);//生成子弹
            b.GetComponent<Bullet>().CapsuleCollider = this.gameObject.GetComponent<CapsuleCollider>();
            b.GetComponent<Bullet>().atk = atk;
        }
        #endregion

        //todo: 生成爆炸特效
    }

    /// <summary>
    /// 走路留下地刺
    /// </summary>
    IEnumerator WalkEffect()
    {
        yield return new WaitForSeconds(0.2f);
        foreach (var item in rootPrefabs)
        {
            if (v != 0 || h != 0)
            Instantiate(item, root.transform.position, root.transform.rotation, EnemyBulletTemp.transform);
        }
        yield return StartCoroutine(WalkEffect());
    }

   // / <summary> 
   /// / 获取经验/升级事件 
   /// / </summary>
    public void getEmpirical()
    {
        score += 10;
        exp += expBall_value;
        //升级逻辑
        if (exp >= 1)
        {
            exp = 0;
            max_exp *= 1.2f;
            SkillManager._instance.EnemygetRandomSkill().Active(gameObject);
        }
    
    }


    /// <summary> 设置拾取范围 </summary>
    public void setPickUpRange(float rangeValue)
    {
        transform.GetComponentInChildren<SphereCollider>().radius = rangeValue;
    }
    /// <summary>
    /// 人物死亡
    /// </summary>
    //public void PlayerDied()
    //{
    //    Destroy(gameObject);
    //    speed = 0;      
    //    Destroy(gameObject.transform.GetChild(0).transform.GetChild(0).gameObject);//人物模型
    //     gameObject.transform.GetChild(2).gameObject.SetActive(false);//血条  
    //    temp= Instantiate(DiedEffect, EnemyCharacter.transform.GetChild(0));//死亡特效
    //    Invoke("Revive",3); 
    //    //todo: 播放爆炸特效
    //}
    /// <summary>
    /// 人物复活
    /// </summary>
    //public void Revive()
    //{
    //    speed = playerBases.speed;
    //    hp = playerBases.max_hp;
    //    lifbar.value = hp / max_hp;
    //    Destroy(temp.gameObject);
    //    temp = Instantiate(enemychara, EnemyCharacter.transform.GetChild(0));
    //    gameObject.transform.GetChild(2).gameObject.SetActive(true);//血条  
    //}

    public void PlayerDied()
    {
        float pos = Random.Range(-1, 1);
        Vector3 temp = new Vector3(transform.position.x + pos, 0, transform.position.z + pos);
        Instantiate(empiricalBall, temp, Quaternion.identity);
        transform.GetComponent<AI>().enabled = false;
        Destroy(gameObject.transform.GetChild(0).transform.GetChild(0).gameObject);//人物模型
        gameObject.transform.GetChild(1).gameObject.SetActive(false);//血条  
        p = Instantiate(DiedEffect,transform.GetChild(0));//死亡特效
        float x = Random.Range(-40, 40);
        float z = Random.Range(-40, 40);
        transform.Translate(new Vector3(x, 0, z));
        //todo: 播放爆炸特效
        Invoke("Revive", 5);
   
    }

    public void Revive()
    {
        
        transform.GetComponent<AI>().enabled = true;
        hp = playerBases.max_hp;
        lifbar.value = hp / max_hp;
        Destroy(p.gameObject);
        p = Instantiate(enemychara, transform.GetChild(0));
        gameObject.transform.GetChild(1).gameObject.SetActive(true);//血条
    }
    /// <summary>
    /// 回血
    /// </summary>
    public void PlusBlood()
    {
        hp += 2;
        lifbar.value = hp / max_hp;
    }

}
